package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.dto.ConsumableApprovalRequest;
import com.carol.lmsbe.dto.ConsumablePlanCreateRequest;
import com.carol.lmsbe.dto.ConsumablePlanDTO;
import com.carol.lmsbe.dto.ConsumablePlanQueryRequest;
import com.carol.lmsbe.service.ConsumablePlanService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 易耗品计划控制器
 */
@RestController
@RequestMapping("/api/consumable-plans")
public class ConsumablePlanController {

    @Autowired
    private ConsumablePlanService consumablePlanService;

    /**
     * 分页查询易耗品计划
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumablePlanDTO>> listPlans(ConsumablePlanQueryRequest request) {
        Page<ConsumablePlanDTO> page = consumablePlanService.listPlans(request);
        return Result.success(page);
    }

    /**
     * 根据ID获取易耗品计划详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<ConsumablePlanDTO> getPlanById(@PathVariable Long id) {
        ConsumablePlanDTO plan = consumablePlanService.getPlanById(id);
        return Result.success(plan);
    }

    /**
     * 创建易耗品计划
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<ConsumablePlanDTO> createPlan(@Valid @RequestBody ConsumablePlanCreateRequest request) {
        ConsumablePlanDTO plan = consumablePlanService.createPlan(request);
        return Result.success(plan);
    }

    /**
     * 更新易耗品计划
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<ConsumablePlanDTO> updatePlan(@PathVariable Long id, @Valid @RequestBody ConsumablePlanCreateRequest request) {
        ConsumablePlanDTO plan = consumablePlanService.updatePlan(id, request);
        return Result.success(plan);
    }

    /**
     * 删除易耗品计划
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<Void> deletePlan(@PathVariable Long id) {
        consumablePlanService.deletePlan(id);
        return Result.success();
    }

    /**
     * 提交易耗品计划
     */
    @PostMapping("/{id}/submit")
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<Void> submitPlan(@PathVariable Long id) {
        consumablePlanService.submitPlan(id);
        return Result.success();
    }

    /**
     * 审批易耗品计划
     */
    @PostMapping("/{id}/approve")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> approvePlan(@PathVariable Long id, @Valid @RequestBody ConsumableApprovalRequest request) {
        consumablePlanService.approvePlan(id, request.getStatus(), request.getComment());
        return Result.success();
    }

    /**
     * 获取我的易耗品计划
     */
    @GetMapping("/my-plans")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumablePlanDTO>> getMyPlans(ConsumablePlanQueryRequest request) {
        Page<ConsumablePlanDTO> page = consumablePlanService.getMyPlans(request);
        return Result.success(page);
    }
}
